package com.itheima.dao;

import com.itheima.pojo.Menu;
import org.apache.ibatis.annotations.*;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

public interface MenuDao {

    @Select("select * from t_menu")
    public List<Menu> getAllMenu();

    @Select("select distinct tm.* from t_user tu,t_role tr,t_menu tm,t_role_menu trm,t_user_role tur " +
            "where tu.id=tur.user_id and tr.id=tur.role_id and tr.id=trm.role_id " +
            "and trm.menu_id=tm.id and tm.level=1")
    public List<Menu> getMenuLevelIsOne();

    @Select("select distinct tm.* from t_user tu,t_role tr,t_menu tm,t_role_menu trm,t_user_role tur " +
            "where tu.id=tur.user_id and tr.id=tur.role_id and tr.id=trm.role_id " +
            "and trm.menu_id=tm.id and tm.level=1 and tm.name like CONCAT('%',#{queryString},'%')")
    public List<Menu> getMenuLevelIsOneByQueryString(String queryString);

    @Select("select distinct\n" +
            "\ttm.* \n" +
            "from \n" +
            "\tt_user tu,t_role tr,t_menu tm,t_role_menu trm,t_user_role tur\n" +
            "where \n" +
            "\ttu.id=tur.user_id\n" +
            "and\n" +
            "\ttr.id=tur.role_id\n" +
            "and\n" +
            "\ttr.id=trm.role_id\n" +
            "and\n" +
            "\ttrm.menu_id=tm.id\n" +
            "and\n" +
            "\ttm.level=2\n" +
            "and\n" +
            "\ttm.parentMenuId=#{menuId}")
    public List<Menu> getByParentMenuIdMenuLevelIsTwo(Integer menuId);


    @Select("select * from t_menu where name like CONCAT('%',#{queryString},'%') ")
    List<Menu> getAllMenuByQueryString(String queryString);

    @Insert("insert into t_role_menu (role_id,menu_id) values (#{roleId},#{menuId})")
    public void insertRoleIdAndMenuId(@Param("roleId") Integer roleId, @Param("menuId") Integer menuId);

    @Select("select * from t_menu")
    public LinkedHashSet<Menu> getByRole(Integer roleId);

    @Select("select menu_id from t_role_menu where role_id=#{roleId}")
    public List<Integer> getByRoleId(Integer roleId);

    @Delete("delete from t_role_menu where role_id=#{roleId}")
    public void deleteRoleIdAndMenu(Integer roleId);

    @Insert("insert into t_menu (name,linkUrl,path,priority,parentMenuId,level,icon,description) " +
            " values (#{name},#{linkUrl},#{path},#{priority},#{parentMenuId},#{level},#{icon},#{description})")
    public void insertMenu(Map map);

    @Select("select * from t_menu where id=#{menuId}")
    public Map findById(Integer menuId);

    @Update("update t_menu set name=#{name},linkUrl=#{linkUrl},path=#{path},priority=#{priority},parentMenuId=#{parentMenuId},level=#{level},icon=#{icon},description=#{description} where id=#{id}")
    void updateMenu(Map map);

    @Delete("delete from t_menu where id=#{menuId}")
    void deleteMenu(Integer menuId);
}
